
/**
 * CategoryService
 *
 * @author glory.xu
 */

import { Service } from 'egg';

export class CategoryService extends Service {


  public async getList({ limit = 10, offset = 1, opt = {} }) {
    const { ctx } = this;
    // 创建任务流模板
    // const offset = (pageNum - 1) * pageSize;
    const { count, rows } = await ctx.model.Category.findAndCountAll({
      where: { ...opt },
      limit,
      offset,
      order: [[ 'id', 'DESC' ]],
    });

    const result = { total: count, rows };
    return result;

  }

  public async get(id) {
    const { ctx } = this;
    const result = await ctx.model.Category.findOne({
      where: {
        id,
      },
    });
    return result;
  }

  public async edit(data = {}, id) {
    const { ctx } = this;
    ctx.model.Category.update({
      ...data,
    }, {
      where: {
        id,
      },
    });
  }

  public async create(data = {}) {
    const { ctx } = this;
    const res = await ctx.model.Category.create({
      ...data,
    });
    return res;
  }

  public async update(data = {}, id) {
    const { ctx } = this;
    ctx.model.Category.update({
      ...data,
    }, {
      where: {
        id,
      },
    });
  }

  public async destroy(id) {
    const { ctx } = this;
    ctx.model.Category.destroy({
      where: {
        id,
      },
    });
  }
}

export default CategoryService;

